﻿using CommonDevelop.Operator;
using CommonDevelop.Scene;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using System.Linq;
using System.Threading.Tasks;

namespace WebAdmin.JWT
{
    /// <summary>
    /// 会话权限过滤
    /// </summary>
    public class CheckRequirement : IAuthorizationRequirement
    {

    }

    /// <summary>
    /// 会话权限过滤
    /// </summary>
    public class CheckHandler : AuthorizationHandler<CheckRequirement>
    {
        private IAuthenticationSchemeProvider _Schemes;
        private IHttpContextAccessor _Accessor;

        /// <summary>
        /// 构造函数注入
        /// </summary>
        public CheckHandler(IAuthenticationSchemeProvider schemes, IHttpContextAccessor Accessor)
        {
            this._Schemes = schemes;
            this._Accessor = Accessor;
        }

        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, CheckRequirement requirement)
        {
            //通过注入取httpContext，用httpContext操作比较有效，方便日后扩展
            var httpContext = this._Accessor.HttpContext;
            if (this.DataAuthorize(httpContext.Request.RouteValues, OperatorProvider.Provider.Current()))
                context.Succeed(requirement);

            return Task.CompletedTask;
        }

        /// <summary>
        /// 执行权限认证
        /// </summary>
        private bool DataAuthorize(RouteValueDictionary Dic, UserInfo Current)
        {
            if (Dic == null || !Dic.Any())
                return false;

            if (Current == null)
                return false;
            else if (Current.IsAdmin)
                return true;

            string areaName = "";
            string controllerName = "";
            string action = "";
            if (Dic.ContainsKey("area"))
                areaName = Dic["area"].ToString() + "/"; //获取当前区域

            if (Dic.ContainsKey("controller"))
                controllerName = Dic["controller"].ToString() + "/"; //获取控制器

            if (Dic.ContainsKey("action"))
                action = Dic["action"].ToString(); //获取当前Action

            string currentUrl = "/" + areaName + controllerName + action;               //拼接构造完整url
            string controller = "/" + areaName + controllerName;

            if (currentUrl.Contains("Home"))
                return true;

            if (currentUrl.Contains("Annexes"))
                return true;

            //判断访问权限
            var MenuData = GlobalData.GetMenu(Current);
            var M = MenuData.FirstOrDefault(a => a.Module_Address.Equals(currentUrl));
            if (M != null)
                return Current.Power.FirstOrDefault(a => a.ModuleId == M.Module_Id) != null;
            else
            {
                M = MenuData.FirstOrDefault(a => a.Module_Address.Contains(controller));
                if (M != null)
                    return Current.Power.FirstOrDefault(a => a.ModuleId == M.Module_Id) != null;
            }
            return true;
        }
    }
}
